package com.zrrd.yunchmall.user.util;

import com.zrrd.yunchmall.pojo.UserTmp;
import com.zrrd.yunchmall.user.entity.Admin;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.UUID;

/**
 * @author Liangyaoyun
 */
@SuppressWarnings("all")
public class JwtUtil {


    private static String salt = "0103";

        /*
        * 生成token令牌的方法，需要令牌id,设置，签发人，主体，签发时间，有效时间
        * Claim需要包括：用户id，用户名
        * @Param exp 令牌的有效时间，单位毫秒
        * @Param userInfo
        * */

    public static String create(long exp, UserTmp userInfo){
        JwtBuilder builder= Jwts.builder();
        //设置id
        builder.setId(UUID.randomUUID().toString());
        //签发人
        builder.setIssuer("ZRYC");
        //签发时间
        builder.setIssuedAt(new Date());
        //过期时间
        builder.setExpiration(new Date(System.currentTimeMillis()+exp));
        //设置主题
        builder.setSubject("云创商城");
        builder.claim("userId",userInfo.getUid());
        builder.claim("userName",userInfo.getUsername());
        //设置加密算法和盐
        builder.signWith(SignatureAlgorithm.HS256,salt);
        return "Bearer " + builder.compact();
    }
    public static String create(long exp, Admin userInfo){
        JwtBuilder builder= Jwts.builder();
        //设置id
        builder.setId(UUID.randomUUID().toString());
        //签发人
        builder.setIssuer("ZRYC");
        //签发时间
        builder.setIssuedAt(new Date());
        //过期时间
        builder.setExpiration(new Date(System.currentTimeMillis()+exp));
        //设置主题
        builder.setSubject("云创商城");
        builder.claim("userId",userInfo.getId());
        builder.claim("userName",userInfo.getUsername());
        //设置加密算法和盐
        builder.signWith(SignatureAlgorithm.HS256,salt);
        return "Bearer " + builder.compact();
    }
    /*
    * 对给定的token进行解析，如果成功对用户信息封装返回，出错返回null
    * @param token
    * @return
    * */
    public static UserTmp parse(String token){
        try {
            Claims claims=Jwts.parser()
                    //设置盐(密钥)
                    .setSigningKey(salt)
                    .parseClaimsJws(token).
                    getBody();
            UserTmp userTmp=new UserTmp();
            userTmp.setUid(claims.get("userId",Integer.class));
            userTmp.setUsername(claims.get("username",String.class));
            return userTmp;
        }catch (Exception ex){
            return null;
        }
    }
    public static Admin parseAdminToken(String token){
        try {
            Claims claims=Jwts.parser()
                    //设置盐(密钥)
                    .setSigningKey(salt)
                    .parseClaimsJws(token).
                    getBody();
            Admin admin=new Admin();
            admin.setId(claims.get("userId",Long.class));
            admin.setUsername(claims.get("username",String.class));
            return admin;
        }catch (Exception ex){
            return null;
        }
    }
}
